<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use DB;
use App\Repositorys\ShopRepository;

class ShopController extends BaseController
{
    public function __construct(Request $request)
    {
        $this->middleware('CheckUserLogin');
    }

    public function put_scrip(Request $request)
    {
        $user = Request()->get('user');

        DB::beginTransaction();
        try {
            $params = $request->all();
            if ($user->gold < Config('common.put_scrip_gold')) return jsonFailed('金币不足');

            // 直接控制器内验证
            if (!isset($params['content']) || empty($params['content'])) {
                return jsonFailed('请输入你想说的话~');
            }
            if (!isset($params['weixin']) || empty($params['weixin'])) {
                return jsonFailed('请输入微信号~');
            }

            $data['sex'] = $params['sex'];
            $data['weixin'] = $params['weixin'];
            $data['content'] = $params['content'];
            $data['user_id'] = $user->id;

            $id = DB::table('user_put_scrip')->insertGetId($data);

            if (isset($params['images']) && !empty($params['images'])) {
                $data_image = [];
                foreach ($params['images'] as $key => $value) {
                    $data_image[$key]['scrip_id'] = $id;
                    $data_image[$key]['image'] = $value;
                }
                DB::table('user_put_scrip_to_image')->insert($data_image);
            }

            DB::table('user')->decrement('gold', Config('common.put_scrip_gold'));
            DB::table('gold_log')->insert(['user_id' => $user->id, 'gold' => Config('common.take_scrip_gold'), 'ident' => 'dec']);
            DB::commit();
            return jsonSuccess();
        } catch (\Throwable $th) {
            DB::rollBack();
            return jsonFailed($th->getMessage());
        }
    }

    public function take_scrip(Request $request)
    {
        DB::beginTransaction();

        try {
            $sex = $request->sex;
            $user = $request->get('user');
            if ($user->gold < Config('common.take_scrip_gold')) return jsonFailed('金币不足');

            $count = app(ShopRepository::class)->getTodayTakeScripCount($user->id);
            if ($count > Config('common.today_take_scrip_max_count')) {
                return jsonFailed('每天最多可抽取' . Config('common.today_take_scrip_max_count') . '张纸条');
            }

            $scrip_ids = DB::table('user_take_scrip')->where('user_id', $user->id)->pluck('scrip_id')->toArray();
            $res = DB::table('user_put_scrip')
                    ->where('sex', $sex)
                    ->whereNotIn('id', $scrip_ids)
                    ->where('user_id', '<>', $user->id)
                    ->inRandomOrder()
                    ->first();

            if (empty($res)) return jsonFailed('箱内没纸条了，等待片刻再来抽取吧');

            DB::table('user_take_scrip')->insertGetId(['user_id' => $user->id, 'scrip_id' => $res->id]);

            DB::table('user')->decrement('gold', Config('common.take_scrip_gold'));
            DB::table('gold_log')->insert(['user_id' => $user->id, 'gold' => Config('common.take_scrip_gold'), 'ident' => 'dec']);
            return jsonSuccess();
        } catch (\Throwable $th) {
            DB::rollBack();
            return jsonFailed($th->getMessage());
        }
    }

    public function getArticle(Request $request)
    {
        $data = app(ShopRepository::class)->article_data[$request->id];
        return jsonSuccess($data);
    }
}
